var MENU_BAR_HASH = {};
var needOpen = [];
(function () {

    /*    var ut = szrcGetLoginUt();
        if(ut == 1) {
            $("#headBar").hide();
        }
    
        szrcAjaxPost("/common/app/menu/load_menu", {}, function(rdata, r) {
            if(Array.isArray(rdata) && rdata.length > 0) {
                setMenuData(rdata);
            } else {
                loadMenuAfter();
            }
    
        });*/
    if (!menuList) {
        menuList = []
    }
    setMenuData(menuList);

})(jQuery);

/**菜单字体颜色 */
function setColor(item) {
    var style = '';
    if (item.color) {
        style = 'color:' + item.color
    }
    return style;
}
/**初始化菜单 */
function setMenuData(list) {
    var menu = {};
    var htm = "";
    var first = "";
    for (var j = 0; j < list.length; j++) {
        var _class = "";
        var item = list[j];
        MENU_BAR_HASH[item["id"]] = item;

        if (j == 0) {
            _class = " active";
            menu = item;
        }
        var fontColor = "";
        if (item.color) {

        }
        htm += '<li class="navItem' + _class + '" data-id="' + item.id + '">' + item.title + '</li>'
    }
    htm += '<li class="line"></li>';
    if (menu.children) {
        $("#leftMenu").width(200);
        init_menu(menu.children);
    } else {
        $.addTab(menu)
    }
    $("#headNav").html(htm);
    var headNavItem = $("#headNav .navItem");
    var firstNavItem = headNavItem.eq(0);
    var headNavLine = $("#headNav .line");
    headNavLine.css({
        "left": firstNavItem.position().left,
        "width": firstNavItem.width()
    });
    $("#headNav li")
        .off("click")
        .on("click", function () {
            var id = $(this).data("id");
            var index = $(this).index()
            headNavLine.css({
                "left": $(this).position().left,
                "width": $(this).width()
            });
            $(this).addClass("active").siblings().removeClass("active");
            //menu = list.find(function (v) { v.id == id });
            var menu = MENU_BAR_HASH[id];
            if (menu) {
                emptyHtml();
                if (menu.children && menu.children.length) {
                    // $(".container_right").css('padding-left', '220px');
                    // $("#treeBox").css('width', 'calc(100% - 200px)');
                    $("#leftMenu").css('width', '200px');
                    init_menu(menu.children, index);
                } else if (menu.path) {
                    $("#leftMenu").css('width', '0');
                    // $("#treeBox").css('width', '100%');
                    // $(".container_right").css('padding-left', '0px');
                    $.addTab(menu);
                }
                loadMenuAfter();
            }
        });
}
function emptyHtml() {
    $("#menuBox").html("");
}

function init_menu(menus, index) {
    // if (!index) index = 0
    if (!Array.isArray(menus)) menus = new Array();
    var chooseItem = {};
    var needOpen = []
    var str = '<ul class="menuItem">';
    var hasOpen_p = menus.find(function (v) { return v.flagOpen });
    var hasFlagExpand = menus.find(function (v) { return v.flagExpand });
    for (var i = 0; i < menus.length; i++) {
        var item = menus[i];
        var li_c_class = "";
        if (item.children && item.children.length) {
            li_c_class = "hasChild";
            if (!hasFlagExpand && (i == 0)) {
                li_c_class += " showMenu";
            } else if (item.flagExpand) {
                li_c_class += " showMenu";
            }
        } else if (!hasOpen_p && i == 0) {
            li_c_class = "active";
            chooseItem = item;
        } else if (item.flagOpen) {
            li_c_class = "active";
            chooseItem = item;
            needOpen.push(item)
        }
        str +=
            '<li class="' +
            li_c_class +
            '"><a class="' +
            (item.icon ? "hasIcon iconfont " + item.icon : "noIcon") +
            '" data-id="' + item.id + '" id="menu_' +
            item.id +
            '" href="javascript:void(0);" data-src="' +
            item.path +
            '" data-name="' +
            item.title +
            '"><span class="menuText" style="' + setColor(item) + '">' +
            item.title;
        if (item.notice) {
            str += '<span class="number">' + item.notice + '</span>'
        }
        str += "</span>";
        var hasOpen_child = item.children && item.children.find(function (v) { return v.flagOpen });
        if (item.children && item.children.length) {
            str += '<i class="iconfont icon-arrow-down"></i></a>';
            str += '<ul class="childMenu">';
            for (var j = 0; j < item.children.length; j++) {
                var item_c = item.children[j];
                var li_class = "";
                if (item_c.children && item_c.children.length) {
                    li_class = "hasChild";
                    if ((j == 0) || item_c.flagExpand) {
                        li_class += " showMenu";
                    }
                } else if (!hasOpen_child && i == 0 && j == 0) {
                    li_class = "active";
                    chooseItem = item_c;
                } else if (item_c.flagOpen) {
                    li_class = "active";
                    chooseItem = item_c;
                    needOpen.push(item_c)
                }
                str +=
                    '<li class="' +
                    li_class +
                    '" title="' +
                    item_c.title +
                    '"><a data-id="' + item_c.id + '" id="menu_' +
                    item_c.id +
                    '" href="javascript:void(0);" class="' +
                    (item_c.icon ? "hasIcon iconfont " + item_c.icon : "noIcon") +
                    '" data-src="' +
                    item_c.path +
                    '" data-name="' +
                    item_c.title +
                    '"><span class="menuText" style="' + setColor(item_c) + '">' +
                    item_c.title;
                if (item_c.notice) {
                    str += '<span class="number">' + (item_c.notice > 99 ? '99+' : item_c.notice) + '</span>'
                }
                str += "</span>";
                var hasOpen_c_child = item_c.children && item_c.children.find(function (v) { return v.flagOpen });
                if (item_c.children && item_c.children.length) {
                    str += '<i class="iconfont icon-arrow-down"></i></a>';
                    str += '<ul class="childMenu">';
                    for (var k = 0; k < item_c.children.length; k++) {
                        var item_child = item_c.children[k];
                        var c_li_class = "";
                        if (!hasOpen_c_child && i == 0 && j == 0 && k == 0) {
                            chooseItem = item_child;
                            c_li_class = 'active'
                        } else if (item_child.flagOpen) {
                            chooseItem = item_child;
                            c_li_class = 'active'
                            needOpen.push(item_child)
                        }
                        str +=
                            '<li title="' + item_child.title + '" class="' + c_li_class + '"><a class="noIcon" id="' + item_child.id + '" data-id="' +
                            item_child.id +
                            '" href="javascript:void(0);" data-src="' +
                            item_child.path +
                            '" data-name="' +
                            item_child.title +
                            '">' +
                            '<text style="' + setColor(item) + '">' +
                            item_child.title +
                            '</text></a></li>';
                    }
                    str += "</ul>";
                } else {
                    str += "</a>";
                }
                str += "</li>";
            }
            str += "</ul>";
        } else {
            str += "</a>";
        }
        // }
        str += "</li>";
    }
    str += "</ul>";
    $("#menuBox").html(str);
    // if (index == 0) {
    //   addTab(chooseItem);
    // }
    if (needOpen.length) {
        needOpen.forEach(function (v) {
            $.addTab(v);
        })
    } else {
        $.addTab(chooseItem);
    }

    loadMenuAfter();
}
function loadMenuAfter() {
    console.log(needOpen)
    if (needOpen.length) {
        needOpen.forEach(function (v) {
            setTimeout(function () {
                v.path && $.addTab(v);
            }, 100);
        })
    }
    $("#menuBox li a")
        .off("click")
        .on("click", function () {
            var li = $(this).parent("li");
            var href = $(this).data("src");
            var id = $(this).data("id");
            var title = $(this).data("name");
            var srcParam = $(this).attr("srcParam");
            var paramData = {};
            if (srcParam) {
                var arr1 = srcParam.split(",");
                if (arr1) {
                    $.each(arr1, function (j1, k1) {
                        if (k1) {
                            var arr2 = k1.split("=");
                            if (arr2 && arr2.length == 2) {
                                paramData[arr2[0]] = arr2[1];
                            }
                        }
                    });
                }
            }
            var hasChild = li.hasClass("hasChild");
            if (hasChild) {
                li.toggleClass("showMenu");
                if (!li.hasClass("showMenu")) {
                    li.find(".showMenu").removeClass("showMenu");
                }
            } else {
                $("#menuBox li").removeClass("active");
                // setTimeout(function () {
                li.addClass("active");
                // }, 100);
                if (title !== "首页") {
                    $.addTab({ title: title, id: id, path: href, data: paramData });
                } else {
                    $("#headTree a:first")
                        .addClass("active")
                        .siblings()
                        .removeClass("active");
                    $("#mainIframe .iframe").addClass("hidden");
                    $("#homePage").removeClass("hidden");
                }
            }
            return;
        });

    $(".closeAll").click(function () {
        $("#headTree").children("a").eq(0).siblings().remove();
        $("#mainIframe")
            .children(".iframe")
            .eq(0)
            .removeClass("hidden")
            .siblings()
            .remove();
        // $("#menuBox").find('.active').removeClass('active');
    });

    //菜单左右点击
    $("#treeBox").off('click', '.treeLeft').on('click', '.treeLeft', function () {
        var p_w = $("#treeBox .treeMain").width();
        var t_w = $("#headTree").width();
        var t_l = $("#headTree").position().left;
        var r_w = t_w - p_w;
        var m_l = Math.abs(t_l)
        if (t_w < p_w) {
            return false;
        }
        if (t_l == 0) {
            return false;
        } else {
            if (m_l <= (p_w / 2)) {
                $("#headTree").css('left', 0)
            } else {
                $("#headTree").css('left', '-' + p_w / 2 + 'px')
            }
        }
    })
    $("#treeBox").off('click', '.treeRight').on('click', '.treeRight', function () {
        var p_w = $("#treeBox .treeMain").width();
        var t_w = $("#headTree").width();//
        var t_l = $("#headTree").position().left;//偏移值
        var m_l = Math.abs(t_l)
        var r_w = t_w - p_w;//差值
        if (t_w < p_w) {
            return false;
        }
        if (t_l > m_l) {
            return false;
        } else {
            if ((r_w - m_l) >= p_w / 2) {
                $("#headTree").css('left', '-' + (t_l + p_w / 2) + 'px')
            } else {
                $("#headTree").css('left', '-' + r_w + 'px')
            }
        }
    })
}
